package com.logic.battle
{
	import com.action.ActionAttack;
	import com.action.ActionBullet;
	import com.action.ActionHurt;
	import com.action.BaseAction;
	import com.net.udp.UDPServer;
	import com.stage.item.Player;
	
	import flash.geom.Point;

	public class BattleServer
	{
		public static var hurtList:Object = {}; // 适当的注意受伤间隔
		public static var minHurtDelay:int = 100;
		
		/**********************************************************************************************
		 * 主机函数，只有被选为主机才能够接收消息
		 * 
		 * 技能使用时，必须先到主机请求
		 * 发出子弹，击中目标由主机进行判断发出消息，非主机客户端不作子弹发出和击中目标的判断
		 * 注意：战斗中移动目前直接由各自客户端向其他客户端发出，如果对同步影响过大需要改为主机请求方式
		 * 
		 * 这里有个大bug，如果用别人id发包，未处理
		 *********************************************************************************************/
		
		/**
		 * 注意，这里的aid应该是根据连接，主机端识别出作为参数传入的
		 * args 是客户机发送的指令包
		 */
		public static function onCommandServer(aid:String, args:Object):void{
			// 必须是主机才能处理客户机指令包
			if(BattleLogic.hostId != BattleLogic.thisPlayer.data.aid){
				return;
			}
			var toType:int = args.toType;
			var player:Player = BattleLogic.playerList[aid];
			if(player && player.action){
				StateMachine.commandState(player.action, toType, args);
			}
		}
		
		
		
//		/**
//		 * 某个角色使用技能必须先到主机请求 
//		 */
//		public static function onAttackServer(aid:String, skillId:String, dir:int):void{
//			if(BattleLogic.hostId != aid){
//				return;
//			}
//			var player:Player = BattleLogic.playerList[aid];
//			if(player){
//				if(player.action is ActionAttack || player.action is ActionHurt){
//					return;
//				}else{
//					// 通过服务端验证，向普通客户端广播
//					UDPServer.getInstance().battleGroup.broadcast("onAttack", aid, skillId, dir);
//				}
//			}
//		}
//		
//		public static function onJumpServer(aid:String, num:int, speed:int, dir:int):void{
//			if(BattleLogic.hostId != aid){
//				return;
//			}
//			var player:Player = BattleLogic.playerList[aid];
//			if(player){
//				if(player.action is ActionHurt || player.action is ActionAttack){
//					return;
//				}else{
//					UDPServer.getInstance().battleGroup.broadcast("onJump", aid, num, speed, dir);
//				}
//			}
//		}
//		
//		public static function onDefenceServer(aid:String, dir:int):void{
//			UDPServer.getInstance().battleGroup.broadcast("onDefence", aid, dir);
//		}
//		
//		public static function onBattleMoveServer(aid:String, dir:int):void{
//			UDPServer.getInstance().battleGroup.broadcast("onBattleMove", aid, dir);
//		}
//		
//		public static function onStandServer(aid:String, dir:int):void{
//			UDPServer.getInstance().battleGroup.broadcast("onStand", aid, dir);
//		}
		
		
		/**********************************************************************************************
		 * 一些基本操作
		 * 主机端才需要的处理
		 * 客户机端不进行这些处理
		 ***********************************************************************************************/
		public static function sendBullet(aid:String, skillId:String, dir:int, x:Number, y:Number):void{
			BattleLogic.bulletIdNum++;
			
			var args:Object = {};
			args.skillId = skillId;
			args.bulletId = BattleLogic.bulletIdNum;
			args.dir = dir;
			args.x = x;
			args.y = y;
			UDPServer.getInstance().battleGroup.broadcast("onBullet", aid, args);
		}
		
		public static function hitTest(obj1:Object, obj2:Object):Boolean{
			if(obj1.x1 > obj2.x2 || obj1.x2 < obj2.x1 || obj1.y1 > obj2.y2 || obj1.y2 < obj2.y1){
				return false;
			}
			return true;
		}
		
		/**
		 * 子弹打中人
		 */
		public static function bulletHitPlayer(bullet:ActionBullet):void{
			var team:Array = BattleLogic.inTeam(bullet.player.data.aid, BattleLogic.team1) ? BattleLogic.team2 : BattleLogic.team1;
			var attRect:Object = bullet.animation.attRect;
			var now:Number = (new Date).time;
			if(attRect && attRect.x1 < attRect.x2){
				var position:Point = bullet.animation.position;
				var attObj:Object = {
					x1 : bullet.animation.x + (attRect.x1 - position.x) * bullet.dir,
						y1 : bullet.animation.y + attRect.y1 - position.y,
						x2 : bullet.animation.x + (attRect.x2 - position.x) * bullet.dir,
						y2 : bullet.animation.y + attRect.y2 - position.y
				};
				if(attObj.x1 > attObj.x2){
					var a:Number = attObj.x2;
					attObj.x2 = attObj.x1;
					attObj.x1 = a;
				}
				if(team){
					for(var i:int=0; i<team.length; i++){
						if(team[i]){
							var player:Player = BattleLogic.playerList[team[i].aid];
							if(player && (!hurtList[player.data.aid] || now - hurtList[player.data.aid] >= minHurtDelay)){
								var hitRect:Object = player.view.getHitRect();
								if(hitRect){
									var pos:Point = player.view.getPosition();
									var hitObj:Object = {
										x1 : player.x + (hitRect.x1 - pos.x) * player.dir,
											y1 : player.y + hitRect.y1 - pos.y,
											x2 : player.x + (hitRect.x2 - pos.x) * player.dir,
											y2 : player.y + hitRect.y2 - pos.y
									};
									if(hitRect.x1 > hitRect.x2){
										var a:Number = hitRect.x2;
										hitRect.x2 = hitRect.x1;
										hitRect.x1 = a;
									} 
									if(hitTest(attObj, hitObj)){
										// 击中
										hurtList[player.data.aid] = now;
										var args:Object = {};
										args.selfId = bullet.player.data.aid;
										args.targetId = player.data.aid;
										args.skillId = bullet.skillId;
										args.dir = bullet.dir;
										args.hurtHp = 100;
										args.bulletId = bullet.bulletId;
										args.toType = BaseAction.HURT;
										UDPServer.getInstance().battleGroup.broadcast("onCommand", BattleLogic.thisPlayer.data.aid, args);
									}
								}
							}
						}
					}
				}
			}
		}
		
		/**
		 * 人打中人
		 */
		public static function playerHitPlayer(action:ActionAttack):void{
			var selfPlayer:Player = action.player;
			var skillId:String = action.skillId;
			var team:Array = BattleLogic.inTeam(selfPlayer.data.aid, BattleLogic.team1) ? BattleLogic.team2 : BattleLogic.team1;
			var attRect:Object = selfPlayer.view.getAttRect();
			var now:Number = (new Date).time;
			if(attRect && attRect.x1 < attRect.x2){
				var position:Point = selfPlayer.view.getPosition();
				var attObj:Object = {
					x1 : selfPlayer.x + (attRect.x1 - position.x) * action.dir,
						y1 : selfPlayer.y + attRect.y1 - position.y,
						x2 : selfPlayer.x + (attRect.x2 - position.x) * action.dir,
						y2 : selfPlayer.y + attRect.y2 - position.y
				};
				if(attObj.x1 > attObj.x2){
					var a:Number = attObj.x2;
					attObj.x2 = attObj.x1;
					attObj.x1 = a;
				}
				if(team){
					for(var i:int=0; i<team.length; i++){
						if(team[i]){
							var player:Player = BattleLogic.playerList[team[i].aid];
							if(player && (!hurtList[player.data.aid] || now - hurtList[player.data.aid] >= minHurtDelay)){
								var hitRect:Object = player.view.getHitRect();
								if(hitRect){
									var pos:Point = player.view.getPosition();
									var hitObj:Object = {
										x1 : player.x + (hitRect.x1 - pos.x) * player.dir,
											y1 : player.y + hitRect.y1 - pos.y,
											x2 : player.x + (hitRect.x2 - pos.x) * player.dir,
											y2 : player.y + hitRect.y2 - pos.y
									};
									if(hitRect.x1 > hitRect.x2){
										var a:Number = hitRect.x2;
										hitRect.x2 = hitRect.x1;
										hitRect.x1 = a;
									} 
									if(hitTest(attObj, hitObj)){
										hurtList[player.data.aid] = now;
										var args:Object = {};
										args.selfId = selfPlayer.data.aid;
										args.targetId = player.data.aid;
										args.skillId = skillId;
										args.dir = action.dir;
										args.hurtHp = 100;
										args.bulletId = 0;
										args.toType = BaseAction.HURT;
										UDPServer.getInstance().battleGroup.broadcast("onCommand", BattleLogic.thisPlayer.data.aid, args);
									}
								}
							}
						}
					}
				}
			}
		}
		
		/**
		 * 人撞上子弹
		 */
		public static function playerComeBullet(player:Player):void{
			
		}
		
		/**
		 * 人撞上人
		 */
		public static function playerComePlayer(player:Player):void{
			
		}
		


	}
}